package polimi.sc.expoquest.screen.point;

import java.util.Collection;

import polimi.sc.expoquest.QuestApplication;
import polimi.sc.expoquest.R;
import polimi.sc.expoquest.screen.DashboardActivity;
import polimi.sc.expoquest.storage.datasources.PlayedGameDataSource;
import polimi.sc.expoquest.storage.model.PlayedGame;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class PointActivity extends Activity {
	//The played game data source
	private PlayedGameDataSource playedGameDS;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_point);
		
		//1.Retrieving the user ID logged into the application
		QuestApplication questApp = (QuestApplication)getApplicationContext();
		int userID = questApp.getUserID();
		
		//2. We fetch the number of points
		if(playedGameDS== null)
			playedGameDS = new PlayedGameDataSource(this);
		playedGameDS.open();
		Collection<PlayedGame> playedGames =  playedGameDS.getAllPlayedGameByUser(userID);
		int totalPoints = 0;
		for (PlayedGame playedGame : playedGames) {
			int points = playedGame.getPoints();
			totalPoints = totalPoints + points;
		}
		//3. Defining the number of coins earned by  the user according to the points that have
		//TODO improve it
		int gainedcoin = totalPoints/10;
		//4. Setting up the UI
		TextView coinText = (TextView) findViewById(R.id.coinText);
		coinText.setText(gainedcoin + " Coins");
		TextView totalpointText = (TextView) findViewById(R.id.totalpointText);
		totalpointText.setText("Total Points: "+ totalPoints);
		
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_point, menu);
		return true;
	}
	public void homepagebtn_click(View v){
		Intent dashbordIntent = new Intent(this, DashboardActivity.class);
        startActivity(dashbordIntent);
	}

}
